//W3C GeoLocation service object based on com.nokia.device.geolocation Interface
var serviceObj = null;
// Variable for watch id. it is used for removing watch.
var watchId = null;

/*
 * Options for retrieving position info
 * PositionOptions schema
 *  {
 *	  "enableHighAccuracy": boolean,  //currently Not Supported
 *    "timeout": number,
 *    "maximumAge": number
 *  }
 */
var positionOpts = null;

function initSystemServices(){

    positionOpts = {
        timeout: 30000,
        maximumAge: 1000
    };
    
    try {
        serviceObj = nokia.device.load("geolocation");
        GetLocationOneShot();
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 * Gets one-time data for hosting device position.
 */
function GetLocationOneShot(){

    try {
        serviceObj.getCurrentPosition(success_callback, error_callback, positionOpts);
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 * Starts permanent monitoring of position and gets notification whenever hosting device position changes
 */
function startMonitoring(){
    try {
        watchId = serviceObj.watchPosition(success_callback, error_callback, positionOpts);
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Stops position monitoring
 */
function stopMonitoring(){
    try {
        if (watchId != null) 
            serviceObj.clearWatch(watchId);
        watchId = null;
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Callback function that is called asynchronously with positionInformation.
 * result - positionInformation
 */
function success_callback(result){
    displayPositionData(result);
}

/*
 * Gets called in case of an error
 */
function error_callback(result){
    alert('Error retrieving position\ncode :' + result.code + '\nmessage: ' + result.message);
	channelmonitoractive = true;
    MonitorToggle_onclick(document.getElementById("MonitorToggle"));
}


